package net.stogi.dsper.ui.client.module.user;

import java.util.List;

import net.stogi.common.gwt.gxt.client.core.util.WidgetUtils;
import net.stogi.common.mvp.core.behavior.Behavior;
import net.stogi.common.mvp.core.model.KeyValues;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;

class UserProfileEditorWidget extends Composite implements UserProfileEditorView {

	private static final FormData DEFAULT_FIELD_LAYOUT_DATA = new FormData("-20");

	static {
		DEFAULT_FIELD_LAYOUT_DATA.setMargins(new Margins(0, 0, 5, 0));
	}

	private final Button addDataSourcesButton = new Button();

	private final Button addTagsButton = new Button();

	private final Button cancelButton = new Button();

	private final FormPanel mainPanel = new FormPanel();

	private final FormButtonBinding mainPanelButtonBinding = new FormButtonBinding(mainPanel);

	private final Button saveButton = new Button();

	private final TextField<String> nameTextField = new TextField<String>();

	private final ListView<ModelData> dataSourceListView = new ListView<ModelData>();

	private final ListView<ModelData> tagListView = new ListView<ModelData>();

	private final ListStore<ModelData> tagStore = new ListStore<ModelData>();

	private final ListStore<ModelData> dataSourceStore = new ListStore<ModelData>();

	public UserProfileEditorWidget() {
		dataSourceListView.setStore(dataSourceStore);
		tagListView.setStore(tagStore);

		initBinding();
		initComposite();
		initLayout();
		initComponent(mainPanel);
	}

	@Override
	public void appear() {
		mainPanel.show();
		// mainPanel.el().fadeIn(FxConfig.NONE);
	}

	@Override
	public void block() {
		mainPanel.setEnabled(false);
	}

	@Override
	public String collectName() {
		return nameTextField.getValue();
	}

	@Override
	public void disappear() {
		// mainPanel.el().fadeOut(FxConfig.NONE);
		mainPanel.hide();
		doCleanUserProfileEditor();
	}

	@Override
	public void displayDataSources(List<KeyValues<Long>> dataSourceList) {
		WidgetUtils.populateStoreWithModel(dataSourceStore, dataSourceList);
	}

	@Override
	public void displayName(String name) {
		nameTextField.setValue(name);
	}

	@Override
	public void displayTags(List<KeyValues<Long>> tagList) {
		WidgetUtils.populateStoreWithModel(tagStore, tagList);
	}

	@Override
	public void setAddDataSourcesBehavior(final Behavior behavior) {
		addDataSourcesButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setAddTagsBehavior(final Behavior behavior) {
		addTagsButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setCancelBehavior(final Behavior behavior) {
		cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setSaveBehavior(final Behavior behavior) {
		saveButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void unblock() {
		mainPanel.setEnabled(true);
	}

	private void doCleanUserProfileEditor() {
		dataSourceStore.removeAll();
		tagStore.removeAll();
		nameTextField.setValue(null);
	}

	private void initBinding() {
		mainPanelButtonBinding.addButton(saveButton);
	}

	private void initComposite() {
		nameTextField.setAllowBlank(false);
		nameTextField.setFieldLabel("Name");

		// dataSourceListView.setAutoHeight(true);
		dataSourceListView.setDisplayProperty("name");
		dataSourceListView.setHeight(100);
		dataSourceListView.setSimpleTemplate("{name}");

		// tagListView.setAutoHeight(true);
		tagListView.setDisplayProperty("value");
		tagListView.setHeight(100);
		tagListView.setSimpleTemplate("{value}");

		addDataSourcesButton.setText("Add Sources");
		addTagsButton.setText("Add Tags");
		cancelButton.setText("Cancel");
		saveButton.setText("Save");

		mainPanel.setHeading("User Profile Editor");
		mainPanel.setFrame(true);
		mainPanel.setHeight(0);
		mainPanel.setWidth("100%");
		mainPanel.setVisible(false);
	}

	private void initLayout() {
		mainPanel.add(nameTextField, DEFAULT_FIELD_LAYOUT_DATA);
		mainPanel.add(dataSourceListView, DEFAULT_FIELD_LAYOUT_DATA);
		mainPanel.add(tagListView, DEFAULT_FIELD_LAYOUT_DATA);

		// HBoxLayout layout = new HBoxLayout();
		// HBoxLayoutData layoutData = new HBoxLayoutData();
		// layoutData.setFlex(1);
		// layoutData.setMargins(new Margins(0, 3, 0, 0));
		//
		// LayoutContainer container = new LayoutContainer(layout);
		//
		// container.add(dataSourceListView, layoutData);
		// container.add(tagListView, layoutData);

		// mainPanel.add(container);
		mainPanel.addButton(addDataSourcesButton);
		mainPanel.addButton(addTagsButton);
		mainPanel.addButton(cancelButton);
		mainPanel.addButton(saveButton);
	}
}
